package ControlLayer;

import java.util.ArrayList;
import DBLayer.DBConnection;
import DBLayer.DBInvoice;
import DBLayer.IFDBInvoice;
import ModelLayer.Invoice;
import ModelLayer.SalesOrder;

public class InvoiceCtr 
{
	public InvoiceCtr()
	{
		
	}
	
	// This method return all the customers invoice in an Arraylist. 
	public ArrayList<Invoice> listAllCustomerInvoice()
	{
		IFDBInvoice dbInv = new DBInvoice();
		ArrayList<Invoice> allInv = new ArrayList<Invoice>();
		allInv = dbInv.listAllInvoiceOnCustomer(false);
		return allInv;
	}
	
	// This method search for an Invoice on Invoice ID.
	public Invoice findInvoice(int invoiceId)
	{
		IFDBInvoice dbInv = new DBInvoice();
		return dbInv.findInvoice(invoiceId, true);
	}
	
	//This method creates an new Invoice in DBInvoice with invoiceID, paymentDate, totalPrice and salesOrder.
	public void insertInvoice(int invoiceId, String paymentDate, double totalPrice, SalesOrder salesOrder)
	{
		Invoice invObj = new Invoice();
		invObj.setInvoiceId(invoiceId);
		invObj.setPaymentDate(paymentDate);
		invObj.setTotalPrice(totalPrice);
		invObj.addSalesOrder(salesOrder);
		
		try{
			DBConnection.startTransaction();
			DBInvoice dbInv = new DBInvoice();
			dbInv.insertInvoice(invObj);
			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//This method delete the Invoice on the Invoice ID
	public void deleteInvoice(int invoiceId)
	{
		DBInvoice dbInvoice = new DBInvoice();
		dbInvoice.deleteInvoice(invoiceId);
	}
	
}
